

#include <session.h>
#include <colorpicker/colorpicker.h>
#include <qfileinfo.h>
#include <qdebug.h>
#include <qdir.h>
#include <qurl.h>



cceSession::cceSession ()
	: QObject ()
	, _materialManager (0)
  , _engine (0)
{
}

cceSession* cceSession::Get ()
{
	static cceSession session;
	return &session;
}

void cceSession::Initialize ()
{
#ifdef Q_WS_X11
  _projectFolder = "/opt/programming/crimson-core-data/test01";
#else
	_projectFolder = "d:/programming/crimson-core-data/test01";
#endif

	_materialManager = new cceMaterialManager ();

  qDebug () << this << "create mainwindow";
  _mainWindow = new cceMainWindow ();



  _mainWindow->resize (1024, 768);
	_mainWindow->setVisible(true);


  _materialOverview = new cceMaterialOverviewDialog (_materialManager, _mainWindow);
  _objectOverview = new cceObjectOverviewDialog(_mainWindow);

  ConnectSignals ();
}

void cceSession::ConnectSignals()
{
  connect (_mainWindow->GetAction(cceMainWindow::Action_ViewMaterialBrowser), SIGNAL(triggered()), this, SLOT(ShowMaterialBrowser()));
  connect (_mainWindow->GetAction(cceMainWindow::Action_ViewObjectBrowser), SIGNAL(triggered()), this, SLOT(ShowObjectBrowser()));
  connect (_mainWindow->GetAction(cceMainWindow::Action_FileExit), SIGNAL(triggered()), this, SLOT(QuitApplication ()));
}


void cceSession::ShowObjectBrowser()
{
  _objectOverview->setVisible(!_objectOverview->isVisible());
}

void cceSession::ShowMaterialBrowser()
{
  _materialOverview->setVisible(!_materialOverview->isVisible());
}

void cceSession::QuitApplication()
{
  _mainWindow->close();
}


QString cceSession::CopyFileToProjectPath(const QString &filename, const QString &subfolder, bool replace)
{
	QUrl url (filename);
	QString localFilename = url.toLocalFile().trimmed();

	QFileInfo fi (localFilename);
  QString finalFilename = _projectFolder + "/" + subfolder + "/" + fi.baseName();
	QString exportName = subfolder + "/" + fi.baseName();
	if (fi.completeSuffix().length() > 0)
		{
			finalFilename += "." + fi.completeSuffix();
			exportName += "." + fi.completeSuffix();
		}

	finalFilename = finalFilename.trimmed();
  exportName = exportName.trimmed();

	// test if the source and destination file are the same... than we MUST not remove/copy
	QFileInfo fi0 (localFilename.trimmed());
	QFileInfo fi1 (finalFilename.trimmed());
	if (fi0 == fi1)
		{
			return exportName;
		}

	if (QFile::exists(finalFilename))
    {
      if (replace)
        {
					if (!QFile::remove(finalFilename))
            {
              return QString::Null ();
            }
        }
      else
        {
					return exportName;
        }
    }

	if (QFile::copy(localFilename, finalFilename))
    {
			return exportName;
    }
  return QString::Null ();
}


QString  cceSession::GetFileName(const QString &localFilename) const
{
  QString filename = _projectFolder + "/" + localFilename;
  return filename.trimmed();
}


ceEngine *cceSession::GetEngine ()
{
  if (!_engine)
    {
      qDebug () << this << "initialize engine";

      ceEngine::Config config;
      memset(&config, 0, sizeof(ceEngine::Config));
      _engine = new ceEngine ();
      _engine->Initialize(config, true);
    }
  return _engine;
}
